package com.can.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.can.reggie.common.CustomException;
import com.can.reggie.dto.SetmealDto;
import com.can.reggie.entity.Setmeal;
import com.can.reggie.entity.SetmealDish;
import com.can.reggie.mapper.SetmealMapper;
import com.can.reggie.service.SetmealDishService;
import com.can.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐,同时保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息,操作setmeal表
        this.save(setmealDto);

        //保存套餐和菜品的关联信息,操作setmeal_dish表
        //遍历dto中的list,把setmealId赋值
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);

    }


    /**
     * 删除套餐(设置id_deleted为1),同时删除套餐和菜品的关联数据(设置id_deleted为1)
     *
     * @param ids
     */
    @Override
    public void removeWithDish(List<Long> ids) {
        //设置套餐全部停售
        Boolean flag = true;

        //遍历每一个套餐id
        for (Long id : ids) {
            //查询套餐状态,确定能否删除
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getId, id).eq(Setmeal::getStatus, 1);
            int count = this.count(lqw);

            if (count > 0) {
                //有套餐状态为启售
                flag = false;

            } else {
                //套餐状态为停售,可以删,则删除套餐表中的数据(设置id_deleted为1)
                //update setmeal set is_deleted where setmealId in (1,2,3)
                LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<Setmeal>();
                luw.set(Setmeal::getIsDeleted, 1).eq(Setmeal::getId, id);
                this.update(luw);

                //可以删,则删除关系表中的数据(设置id_deleted为1)
                //update setmeal_dish set is_deleted where setmealId in (1,2,3)
                LambdaUpdateWrapper<SetmealDish> luw1 = new LambdaUpdateWrapper<>();
                luw1.set(SetmealDish::getIsDeleted, 1).eq(SetmealDish::getSetmealId, id);
                setmealDishService.update(luw1);
            }
        }

        if (!flag) {
            //有套餐状态为启售,则抛出业务异常
            throw new CustomException("若套餐正在售卖中,则不能删除");
        }

    }


    /**
     * 根据id查询套餐所有信息
     *
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDto getData(Long setmealId) {
        //创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();

        //根据id查询套餐 基本信息 拷贝给setmealDto对象
        Setmeal setmeal = this.getById(setmealId);
        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据id查询套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<SetmealDish>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(lqw);

        //讲id对应的菜品集合,添加至setmealDto对象
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }


    /**
     * 根据id,修改套餐表和关联菜品表
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        //根据id,修改套餐表
        this.updateById(setmealDto);

        //获取套餐Id
        Long setmealId = setmealDto.getId();

        //根据套餐id, 删除套餐关联菜品表
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lqw);

        //添加新数据,至套餐关联菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

}



